package co.wangdao.xingshaoye.view.fragment;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.astuetz.PagerSlidingTabStrip;
import java.text.SimpleDateFormat;
import java.util.Date;

import co.wangdao.xingshaoye.R;
import co.wangdao.xingshaoye.adapter.TabFragmentAdapter;
import co.wangdao.xingshaoye.utils.Utils;
import co.wangdao.xingshaoye.view.activity.LocationActivity;
import co.wangdao.xingshaoye.view.activity.SearchActivity;

/**
 * Created by Administrator on 2016/2/25 0025.
 */
public class Fragment_main extends Fragment implements View.OnClickListener,AMapLocationListener {
    private Activity activityMain;
    private Context context;
    private View layout;
    private Fragment newfragment;
    //定位
    private TextView positionTextView;
    //位置信息
    private String positonString;
    private String positon; //从定位界面回传的位置信息

    //声明AMapLocationClient类对象
    public AMapLocationClient mLocationClient;
    private AMapLocationClientOption locationOption ;

    @Override
    public void onClick(View v) {

    }

    private void initViews() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if ( resultCode == Activity.RESULT_OK){
            positon =data.getExtras().getString("positonString");//得到新Activity关闭后返回的数据

            mHandler.sendEmptyMessage(Utils.MSG_TOP_LOCATION_STOP);
        }


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (layout == null) {
            context = this.getContext();
            activityMain = this.getActivity();
            layout = activityMain.getLayoutInflater().inflate(R.layout.layout_main, null);
            initViews();
            initData();
            setOnListener();

            ImageView searchview = (ImageView) layout.findViewById(R.id.main_search);
            searchview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activityMain, SearchActivity.class);
                    startActivity(intent);
                }
            });

            TextView location = (TextView) layout.findViewById(R.id.positon);
            location.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activityMain, LocationActivity.class);
                    startActivityForResult(intent,1);
                }
            });


            ViewPager pager = (ViewPager) layout.findViewById(R.id.viewPager);
            pager.setAdapter(new TabFragmentAdapter(getChildFragmentManager()));
            //向ViewPager绑定PagerSlidingTabStrip
            PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) layout.findViewById(R.id.tabs);
            //设置字体大小颜色
            tabs.setTextSize(45);
            tabs.setTextColor(0xFFCECECE);
            tabs.setViewPager(pager);

            //初始化定位
            mLocationClient = new AMapLocationClient(getActivity().getApplicationContext());
            locationOption = new AMapLocationClientOption();
            // 设置定位模式为高精度模式
            locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            locationOption.setInterval(1000*60*10);
            //设置定位回调监听
            mLocationClient.setLocationListener(this);
            mLocationClient.setLocationOption(locationOption);
            mLocationClient.startLocation();
            positionTextView = (TextView) layout.findViewById(R.id.positon);
            positionTextView.setText(positonString);

        }
        return layout;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }



    private void setOnListener() {
        // TODO Auto-generated method stub

    }

    private void initData() {
        // TODO Auto-generated method stub

    }


    Handler mHandler = new Handler() {
        public void dispatchMessage(android.os.Message msg) {
            switch (msg.what) {
                //开始定位
                case Utils.MSG_LOCATION_START:

                    break;
                // 定位完成
                case Utils.MSG_LOCATION_FINISH:
//                    AMapLocation loc = (AMapLocation) msg.obj;
//                    String result = Utils.getLocationStr(loc);
                    positionTextView.setText(positonString);//开启应用时第一次定位更新ui
                    break;
                //停止定位
                case Utils.MSG_LOCATION_STOP:
                    //tvReult.setText("定位停止");
                    break;
                case Utils.MSG_TOP_LOCATION_STOP:
                    //tvReult.setText("回传的位置信息");
                    positionTextView.setText(positon);//定位-定位到当前位置返回信息更新主界面ui
                    break;
                default:
                    break;


            }
        };
    };

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功回调信息，设置相关消息
                aMapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                aMapLocation.getLatitude();//获取纬度
                aMapLocation.getLongitude();//获取经度
                aMapLocation.getAccuracy();//获取精度信息
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date date = new Date(aMapLocation.getTime());
                df.format(date);//定位时间
                positonString = aMapLocation.getAddress();//地址，如果option中设置isNeedAddress为false，则没有此结果，网络定位结果中会有地址信息，GPS定位不返回地址信息。
                aMapLocation.getCountry();//国家信息
                aMapLocation.getProvince();//省信息
                aMapLocation.getCity();//城市信息
                aMapLocation.getDistrict();//城区信息
                aMapLocation.getStreet();//街道信息
                aMapLocation.getStreetNum();//街道门牌号信息
                aMapLocation.getCityCode();//城市编码
                aMapLocation.getAdCode();//地区编码
                mHandler.sendEmptyMessage(Utils.MSG_LOCATION_FINISH);
            } else {
                //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                Log.e("AmapError","location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }

}
